<?php
ob_start();
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

// get the trail ID from the DB using the name
$query="select ID from Trail where name like '$_POST[trailName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from Trail: " . mysql_error();
  die("");
}
$trailID=mysql_result($resultID,0, "ID");

// get the trail ID from the DB using the name
$query="select headID from Trail where name like '$_POST[trailName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting headID from Trail: " . mysql_error();
  die("");
}
$headID=mysql_result($resultID,0, "headID");

// get the user ID from the DB using the name
$query="select ID from Account where userName like '$_POST[userName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from Account: " . mysql_error();
  die("");
}
$userID=mysql_result($resultID,0, "ID");

$query = "
INSERT INTO Saved (headID, trailID, userID)
VALUES (
	$headID,
	$trailID,
	$userID
	)";

if (!mysql_query($query)) {
  echo "Error inserting entry into Saved: " . mysql_error();
}

mysql_close();
header("Location: index.php");
ob_flush();
?> 
